package com.huosu.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huosu.domain.ClassVideo;
import com.huosu.domain.miner.MinerCount;
import com.huosu.domain.miner.MinerMonitorQuery;
import com.huosu.domain.miner.MinerMonitorVo;
import com.huosu.domain.miner.MinerQuery;
import com.huosu.vo.MinerReboot;
import com.huosu.vo.WorkingInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author zhangjian
 * @since 2019-12-12
 */
public interface ClassVideoMapper extends BaseMapper<ClassVideo> {

    /**
     * 实时监控
     *
     * @param page
     * @param query
     * @return
     */
    IPage<ClassVideo> selectMinerPage(Page<ClassVideo> page, @Param("dto") MinerQuery query, @Param("model") String model, @Param("state") String state);

    /**
     * 监控统计连接数详情
     *
     * @param page
     * @param query
     * @return
     */
    @SqlParser(filter = true)
    IPage<MinerMonitorVo> selectPageMonitor(Page<MinerMonitorVo> page, @Param("flag") Integer flag, @Param("dto") MinerMonitorQuery query);

    /**
     * 监控统计连接数详情-不分页
     *
     * @param query
     * @return
     */
    @SqlParser(filter = true)
    List<MinerMonitorVo> selectPageMonitor(@Param("flag") Integer flag, @Param("dto") MinerMonitorQuery query);

    /**
     * 监控模式下，获取所有异常矿机
     *
     * @return
     */
    @Select("select ip from m_miner where run_state=1 and healthy_state in (2,3,6)")
    List<Map<String, String>> getVirusMinersWithOutProtectByMode1();

    /**
     * 获取未开启防护的异常矿机
     *
     * @return
     */
    @Select("select m.ip from m_miner m inner join m_excel e on m.ip=e.ip where m.run_state=1 and m.healthy_state in (2,3,6) and e.switch_flg=1")
    List<Map<String, String>> getVirusMinersWithOutProtectByMode23();

    @Update("update m_miner set run_state=0,virus_addr=null,virus_miner=null,update_time=update_time,refuse_rate='0',hashrate='0',hashrate_avg='0',healthy_state=1 where 1=1")
    void clearAll();

    @Delete("delete from m_miner_reboot where reboot_time <= curdate()")
    void clearAllReboot();

    //离线消息,处理当天不再提示
    @Update("update m_notice set is_tips=1 where type=4 and publish_time<= curdate() and is_tips=2")
    void clearAllOffNotice();

    /**
     * 实时监控矿机数量统计
     *
     * @param query
     * @return
     */
    MinerCount getMinerCount(@Param("dto") MinerQuery query, @Param("model") String model);

    /**
     * 获取需要重启的矿机
     *
     * @param min
     * @return
     */
    List<ClassVideo> getNeedRebootMiners(@Param("min") int min);

    /**
     * 获取矿机重启记录
     *
     * @param ip
     * @param time
     * @return
     */
    @Select("select * from m_miner_reboot where ip = #{ip} and reboot_time > #{time}")
    List<MinerReboot> getMinerRebootList(@Param("ip") String ip, @Param("time") String time);

    /**
     * 查询遗漏的离线矿机：在线并且最后提交share时间是12分钟之前
     * 4个share
     */
//    @Select("select * from m_miner where run_state=1 and update_time < date_sub(now(),interval 5 minute)")
    @Select("select ip from m_miner where run_state=1 " +
            "and " +
            "last_sharetime <= date_sub(now(),interval 12 minute) " +
            "and " +
            "update_time <= date_sub(now(),interval 12 minute)")
    List<ClassVideo> getLostOffMiners();

    @Select("select ip from m_miner where run_state=0 union all select ip from m_miner where last_sharetime is null")
    List<ClassVideo> getOffMiners();

    /**
     * 遗漏的离线矿机:更成离线
     *
     * @param list
     */
    @Update("<script>" +
            "update m_miner set run_state=0,stop_time=now(),virus_addr=null,virus_miner=null," +
            "update_time=update_time,refuse_rate='0',hashrate='0',hashrate_avg='0',healthy_state=1 " +
            "where run_state=1 and ip in " +
            "<foreach item=\"item\" collection=\"list\" open=\"(\" separator=\",\" close=\")\">" +
            "#{item.ip}" +
            "</foreach>" +
            "</script>")
    void updateLostToOff(@Param("list") List<ClassVideo> list);

    /**
     * 实时监控-导出查询
     *
     * @param flag
     * @return
     */
    List<ClassVideo> selectMinerList(@Param("flag") Integer flag);

    /**
     * 矿机当天重启记录
     *
     * @param ip
     * @return
     */
    @Select("select * from m_miner_reboot where ip=#{ip} order by reboot_time desc")
    List<MinerReboot> getRebootHistory(@Param("ip") String ip);

    /**
     * 矿机当天重启记录
     *
     * @param ips
     * @return
     */
    @Delete("<script>" +
            "delete from m_miner where run_state=0 and ip in " +
            "<foreach collection=\"list\" item=\"item\" open=\"(\" separator=\",\" close=\")\">" +
            " #{item}" +
            "</foreach>" +
            "</script>")
    int delMiners(@Param("list") List<String> ips);

    /**
     * 删除错误IP的矿机
     *
     * @param list
     * @return
     */
    @Delete("<script>" +
            "delete from m_miner where ip in " +
            "<foreach collection=\"list\" item=\"item\" open=\"(\" separator=\",\" close=\")\">" +
            " #{item.ip}" +
            "</foreach>" +
            "</script>")
    int delErrorIpMiners(@Param("list") List<ClassVideo> list);

    /**
     * 删除错误IP的EXCEL
     *
     * @param list
     * @return
     */
    @Delete("<script>" +
            "delete from m_excel where ip in " +
            "<foreach collection=\"list\" item=\"item\" open=\"(\" separator=\",\" close=\")\">" +
            " #{item.ip}" +
            "</foreach>" +
            "</script>")
    int delErrorIpExcel(@Param("list") List<ClassVideo> list);

    /**
     * 删除离线超过一天的矿机
     *
     * @return
     */
    @Delete("delete from m_miner where run_state=0 and ifnull(stop_time,'1900-01-01 00:00:00') <= date_sub(now(),interval 1 day)")
    void delOffMinersWithDay();

    @Insert("insert into m_miner_reboot(ip,auto) values(#{ip},#{auto})")
    int saveReboot(@Param("ip") String ip, @Param("auto") int auto);

    @Insert("<script>" +
            "insert into m_miner_reboot(ip,auto) values " +
            "<foreach collection=\"ips\" item=\"ip\" separator=\",\">" +
            "  (#{ip},#{auto})" +
            "</foreach> " +
            "</script>")
    int batchSaveReboot(@Param("ips") List<String> ips, @Param("auto") int auto);

    @Insert("<script>" +
            "insert into m_miner_reboot(ip,auto) values " +
            "<foreach collection=\"list\" item=\"item\" separator=\",\">" +
            "  (#{item.ip},#{auto})" +
            "</foreach> " +
            "</script>")
    int batchSaveRebootAuto(@Param("list") List<ClassVideo> list, @Param("auto") int auto);

    @Insert("insert into m_miner_reboot(ip,auto) select #{ip},#{auto} from dual " +
            "where not exists (select * from m_miner_reboot where ip=#{ip} and reboot_time > date_sub(now(),interval 2 minute))")
    int saveRebootWithInterval(@Param("ip") String ip, @Param("auto") int auto);

    @Update("update m_miner_reboot set history=1 where ip=#{ip} and auto=1")
    int clearReboot(@Param("ip") String ip);

    /**
     * 实时数量
     *
     * @return
     */
    WorkingInfo getWorkingInfoCountMode1();

    /**
     * 实时数量
     *
     * @return
     */
    WorkingInfo getWorkingInfoCountMode23();

}
